#ifndef ADMIN_H_
#define ADMIN_H_

# include "stdio.h"
# include "stdlib.h"
# include "string.h"
# include "windows.h"

# include "all_struct.h"
# include "display.h"
# include "judge.h"
# include "tool.h"

/*
	此部分函数由周珂宇、徐顺杰、栗浩越共同完成
	周：后期整理优化+补充note注释
*/
//# define AdminAccount "admin1234"  管理员密码

//来自meun.h 直接调用
int alterAirInterface(int key);

/*/
本次课设最基本要求所在：增删改函数coding
*/

//录入航班信息 (管理员)
static void insertAirplane(Airplane* airplaneHead)
{
	char TorF = 'Y';
	while(TorF == 'Y' || TorF == 'y')
	{
		Airplane *newAirplane = (Airplane *) malloc(sizeof(Airplane));
		printf("航班号：");
		while(1)
		{
			fflush(stdin);
			scanf("%s",&newAirplane->flightNumber);
			if(judgementAirplane(newAirplane->flightNumber,airplaneHead))
			{
				printf("航班号已经存在，请重新输入：");
			}
			else
			{
				break;
			}
		}

		printf("起飞城市（字符数不得超24个，多余字符将自动舍去）：");
		fflush(stdin);
		s_gets(newAirplane->startCity, 24);
		printf("抵达城市（字符数不得超24个，多余字符将自动舍去）：");
		fflush(stdin);
		s_gets(newAirplane->endCity, 24);


		printf("所在年:");
		fflush(stdin);
		scanf("%d",&newAirplane->time.year);
		printf("所在月:");
		fflush(stdin);
		scanf("%d",&newAirplane->time.month);
		printf("所在日:");
		fflush(stdin);
		scanf("%d",&newAirplane->time.day);
		printf("起飞时间（小时: 00-23）");
		fflush(stdin);
		scanf("%d",&newAirplane->time.original_hour);
		printf("起飞时间（分钟: 00-60）");
		fflush(stdin);
		scanf("%d",&newAirplane->time.original_minute);
		printf("抵达时间（小时: 00-23）");
		fflush(stdin);
		scanf("%d",&newAirplane->time.end_hour);
		printf("抵达时间（分钟: 00-60）");
		fflush(stdin);
		scanf("%d",&newAirplane->time.end_minute);

		printf("所属公司（字符数不得超24个，多余字符将自动舍去）：");
		fflush(stdin);
		s_gets(newAirplane->company, 24);
		printf("所属型号（字符数不得超24个，多余字符将自动舍去）：");
		fflush(stdin);
		s_gets(newAirplane->type, 24);

		printf("航班票价(不能为负数)：");
		fflush(stdin);
		scanf("%d",&newAirplane->price);
		while (0 > newAirplane->price)
		{
			fprintf (stdout,"ERROR:航班票价不能为负数，请您重新输入：");
			fflush(stdin);
			scanf("%d",&newAirplane->price);
		}

		printf("票价折扣(0~1)：");
		while(1)
		{
			fflush(stdin);
			scanf("%lf",&newAirplane->discount);
			if(newAirplane->discount < 0 || newAirplane->discount > 1)
			{
				printf("折扣输入的数值在0~1之间，重新输入：");
			}
			else
				break;

		}

		printf("总票量（[1-40]，若不规范输入，系统将默认票量为40张整）：");
		fflush(stdin);
		scanf("%d",&newAirplane->ticketQuantity);
		if (0 >= newAirplane->ticketQuantity || newAirplane->ticketQuantity > 40)
			newAirplane->ticketQuantity = 40 ;

//		inti.h ---airplane_basicINFO
		newAirplane->personNumber = 0;
		newAirplane->remainQuantity = newAirplane->ticketQuantity - newAirplane->personNumber;
		newAirplane->next = airplaneHead->next;
		//接入链表末尾
		airplaneHead->next = newAirplane;

		printf("录入成功！是否继续录入？(Y/N): ");
		fflush(stdin);
		scanf("%c",&TorF);

	}
}
//修改航班信息 (管理员)
static void alterAirplane(Airplane* airplaneHead )
{
	char flightNumber[25] ;
	printf("请输入航班号进行修改：");
	fflush(stdin);
	scanf("%s",flightNumber);
	Airplane *p = airplaneHead->next;
	while(p)
	{
		if(0 == strcmp(p->flightNumber,flightNumber) && p->personNumber == 0)
		{
			int key;//修改对应选项
			key = alterAirInterface(key);

			if (0 ==key || 1 ==key)
			{
				printf("起飞城市（字符数不得超24个，多余字符将自动舍去）：");
				fflush(stdin);
				s_gets(p->startCity, 24);

				printf("抵达城市（字符数不得超24个，多余字符将自动舍去）：");
				fflush(stdin);
				s_gets(p->endCity, 24);
			}

			if (0 ==key || 2 ==key)
			{
				printf("所在年:");
				fflush(stdin);
				scanf("%d",&p->time.year);
				printf("所在月:");
				fflush(stdin);
				scanf("%d",&p->time.month);
				printf("所在日:");
				fflush(stdin);
				scanf("%d",&p->time.day);
				printf("起飞时间（小时: 1-24）");
				fflush(stdin);
				scanf("%d",&p->time.original_hour);
				printf("起飞时间（分钟: 0-60）");
				fflush(stdin);
				scanf("%d",&p->time.original_minute);
				printf("抵达时间（小时: 1-24）");
				fflush(stdin);
				scanf("%d",&p->time.end_hour);
				printf("抵达时间（分钟: 0-60）");
				fflush(stdin);
				scanf("%d",&p->time.end_minute);
			}

			if(0 ==key || 3 ==key)
			{
				printf("所属公司（字符数不得超24个，多余字符将自动舍去）：");
				fflush(stdin);
				s_gets(p->company, 24);
			}

			if(0 ==key || 4 ==key)
			{
				printf("所属型号（字符数不得超24个，多余字符将自动舍去）：");
				fflush(stdin);
				s_gets(p->type, 24);
			}

			if(0 ==key || 5 ==key)
			{
				printf("航班票价(不能为负数)：");
				fflush(stdin);
				scanf("%d",&p->price);
				while (0 > p->price)
				{
					fprintf (stdout,"ERROR:航班票价不能为负数，请您重新输入：");
					fflush(stdin);
					scanf("%d",&p->price);
				}

			}

			if(0 ==key || 6 ==key)
			{
				printf("票价折扣(0~1)：");
				while(1)
				{
					fflush(stdin);
					scanf("%lf",&p->discount);
					if(p->discount < 0 || p->discount > 1)
						printf("折扣输入的数值在0~1之间，重新输入：");

					else
						break;
				}
			}

			if(0 ==key || 7 ==key)
			{
				printf("总票量（[1-40]，若不规范输入，系统将默认票量为40张整）：");
				fflush(stdin);
				scanf("%d",&p->ticketQuantity);
				if (0 >= p->ticketQuantity || p->ticketQuantity > 40)
					p->ticketQuantity = 40 ;
				p->remainQuantity = p->ticketQuantity - p->personNumber;
			}

			if (8 == key)
			{
				printf ("已取消修改指令，正在返回上一级页面中，请稍后~...\n");
				break ;
			}

			printf("航班信息修改成功！\n");
			break;
		}

		else if(0 == strcmp(p->flightNumber,flightNumber) && p->personNumber > 0)
		{
			printf("该航班已有%d个旅客，不可随意修改，请联系客户协商.....\n",p->personNumber);
			break;
		}

		p = p->next;

	}

	if(p == NULL)
		printf("抱歉！没有查询到对应的航班...\n");

	system("pause");
}
//删除航班 (管理员)
static void deleteAirplane(Airplane* airplaneHead)
{
	char flightNumber[25];
	printf("请输入航班号进行删除：");
	fflush(stdin);
	scanf("%s",flightNumber);
	Airplane *pre = airplaneHead;
	Airplane *p = airplaneHead->next;
	while(p)
	{
		if(0 == strcmp(p->flightNumber,flightNumber) && p->personNumber == 0)
		{
			pre->next = p->next;
			free(p);
			printf("删除成功！\n");

			break;
		}
		else if(0 == strcmp(p->flightNumber,flightNumber) && p->personNumber > 0)
		{
			printf("该航班已有%d个旅客，不可删除，请联系客户协商.....\n",p->personNumber);
			break;
		}
		pre = pre->next;
		p = p->next;
	}
	if(p == NULL)
	{
		printf("抱歉！没有查询到对应的航班...\n");
	}
	p = NULL;
	system("pause");

}


# endif